<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hl:rx:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hl:rx:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hl:rx:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hl:rx:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="rxList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="his处方号" align="center" prop="patientPresId" />
      <el-table-column label="处方标识" align="center" prop="presUniqueId" />
      <el-table-column
        label="处方警示级"
        align="center"
        prop="highestWarningLevel"
      />
      <el-table-column
        label="处方问题数量"
        align="center"
        prop="warningCount"
      />
      <el-table-column label="处方来源" align="center" prop="prescSource">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.source_of_prescription"
            :value="scope.row.prescSource"
          />
        </template>
      </el-table-column>
      <el-table-column label="用药理由" align="center" prop="prescRemark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="来源医院" align="center" prop="partner" />
      <!-- <el-table-column label="处方签" align="center" prop="sigurl">
        <template slot-scope="scope">
          <el-button
            size="mini" 
            icon="el-icon-document"
            @click="
              openPDF(scope.row.sigUrl, `${scope.row.presUniqueId}处方笺.pdf`)
            "
            v-if="scope.row.sigUrl"
            >查看处方笺</el-button
          ></template
        ></el-table-column
      > -->
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_audit_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="
              openPDF(scope.row.sigUrl, `${scope.row.presUniqueId}处方笺.pdf`)
            "
            v-hasPermi="['hl:rx:edit']"
            v-if="scope.row.sigUrl"
            >处方笺</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleEditTable(scope.row)"
            v-hasPermi="['hl:rx:edit']"
            >详情</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hl:rx:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合理用药-处方信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="his处方号" prop="patientPresId">
          <el-input
            v-model="form.patientPresId"
            placeholder="请输入his处方号"
          />
        </el-form-item>
        <el-form-item label="处方标识" prop="presUniqueId">
          <el-input v-model="form.presUniqueId" placeholder="请输入处方标识" />
        </el-form-item>
        <el-form-item label="处方警示级" prop="highestWarningLevel">
          <el-input
            v-model="form.highestWarningLevel"
            placeholder="请输入处方警示级"
          />
        </el-form-item>
        <el-form-item label="处方问题数量" prop="warningCount">
          <el-input
            v-model="form.warningCount"
            placeholder="请输入处方问题数量"
          />
        </el-form-item>
        <el-form-item label="处方来源" prop="prescSource">
          <el-select v-model="form.prescSource" placeholder="请选择处方来源">
            <el-option
              v-for="dict in dict.type.source_of_prescription"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用药理由" prop="prescRemark">
          <el-input v-model="form.prescRemark" placeholder="请输入用药理由" />
        </el-form-item>
        <el-form-item label="来源医院" prop="partner">
          <el-input v-model="form.partner" placeholder="请输入来源医院" />
        </el-form-item>
        <el-form-item label="处方签" prop="sigurl">
          <el-input v-model="form.sigurl" placeholder="请输入处方签" />
        </el-form-item>
        <el-form-item
          label="审核状态(1:已审核 2:待审核 3:已取消)"
          prop="status"
        >
          <el-select
            v-model="form.status"
            placeholder="请选择审核状态(1:已审核 2:待审核 3:已取消)"
          >
            <el-option
              v-for="dict in dict.type.yy_audit_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRx, getRx, delRx, addRx, updateRx } from "@/api/hl/rx";

export default {
  name: "Rx",
  dicts: ["source_of_prescription", "yy_audit_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合理用药-处方信息表格数据
      rxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patientPresId: [
          { required: true, message: "his处方号不能为空", trigger: "blur" },
        ],
        presUniqueId: [
          { required: true, message: "处方标识不能为空", trigger: "blur" },
        ],
        highestWarningLevel: [
          { required: true, message: "处方警示级不能为空", trigger: "blur" },
        ],
        warningCount: [
          { required: true, message: "处方问题数量不能为空", trigger: "blur" },
        ],
        prescSource: [
          { required: true, message: "处方来源不能为空", trigger: "change" },
        ],
        prescRemark: [
          { required: true, message: "用药理由不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        partner: [
          { required: true, message: "来源医院不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    openPDF(base64String, filename) {
      const url = "data:application/pdf;base64," + base64String;
      const newWindow = window.open();
      newWindow.document.write(
        '<iframe width="100%" height="100%" src="' + url + '"></iframe>'
      );
    },
    /** 查询合理用药-处方信息列表 */
    getList() {
      this.loading = true;
      listRx(this.queryParams).then((response) => {
        this.rxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientPresId: null,
        presUniqueId: null,
        highestWarningLevel: null,
        warningCount: null,
        prescSource: null,
        prescRemark: null,
        createTime: null,
        partner: null,
        sigurl: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.patientPresId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合理用药-处方信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patientPresId = row.patientPresId || this.ids;
      getRx(patientPresId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合理用药-处方信息";
      });
    },
    /** 修改按钮操作 */
    handleEditTable(row) {
      const id = row.patientPresId;
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage("详情", "/rx/detail/index/" + id, params);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.patientPresId != null) {
            updateRx(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRx(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patientPresIds = row.patientPresId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除合理用药-处方信息编号为"' +
            patientPresIds +
            '"的数据项？'
        )
        .then(function () {
          return delRx(patientPresIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "hl/rx/export",
        {
          ...this.queryParams,
        },
        `rx_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
